﻿function isDate(txtDate) {
    var currVal = txtDate;
    if (currVal == '')
        return false;

    //Declare Regex  
    var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var dtArray = currVal.match(rxDatePattern); // is format OK?

    if (dtArray == null)
        return false;

    //Checks for dd/mm/yyyy format.
    dtMonth = dtArray[3];
    dtDay = dtArray[1];
    dtYear = dtArray[5];

    if (dtMonth < 1 || dtMonth > 12)
        return false;
    else if (dtDay < 1 || dtDay > 31)
        return false;
    else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
        return false;
    else if (dtMonth == 2) {
        var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
        if (dtDay > 29 || (dtDay == 29 && !isleap))
            return false;
    }
    return true;
}

function verifyEmail(email1) {
   var status = false;
   var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
   if (email1.search(emailRegEx) == -1) {
      alert("Please enter a valid email address.");
   }
   else {

      status = true;
   }
   return status;
}
function getKeyCode(e) {
    if (window.event) {
        return window.event.keyCode; // IE
    }
    return e.which; // Firefox
}

function cancelEvent(e) {
    if (window.event) { // IE
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    return false;
}

function isNumberKey(e) { // input 0-9
    var key = getKeyCode(e);
    if (key >= 48 && key <= 57) {
        return true;
    }
    return cancelEvent(e);
}
     
function isNumberPerid(e) { // input / และ 0-9
    var key = getKeyCode(e);
    if (key >= 47 && key <= 57) {
        return true;
    }
    return cancelEvent(e);
}

function isNumberCode(e) { // input A-Z ,a-zและ 0-9
    var key = getKeyCode(e);
    if ((key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key >= 48 && key <= 57)) {
        return true;
    }
    return cancelEvent(e);
}

// Hook onkeydown on the document
document.onkeydown = function(e) {
    var evt = window.event ? window.event : e;
    var target = evt.target ? evt.target : evt.srcElement;
    if (evt.keyCode==13 && target.onkeydown==null && typeof(target.type)!="undefined") {
        if (target.type=="text" || target.type=="select-one" || target.type=="select-multiple" || target.type=="checkbox" || target.type=="radio" || target.type=="password") {
            // for input element, change enter to tab
            evt.keyCode = 9;
        } else if (target.type=="button" || target.type=="submit" || target.type=="image") {
            // allow enter to submit the page
        } else {
            return cancelEvent(e);
        }
    }
}







// validate

function validateRequireTextBox(id, errmsg) {
  var txt = document.getElementById(id);
  if (txt && txt.value=='') {
    alert(errmsg);
    txt.focus();
    return cancelEvent();
  }
  return true;
}

function validateRequireTextBoxPackage(id,idfocus, errmsg) {
  var bt = document.getElementById(idfocus);
  var txt = document.getElementById(id);
  if (txt && txt.value=='') {
    alert(errmsg);
    bt.focus();
    return cancelEvent();
  }
  return true;
}

function validateLenTextBox(id, len, errmsg) {
  var txt = document.getElementById(id);
  if (txt && txt.value.length!=len) {
    alert(errmsg);
    txt.focus();
    return cancelEvent();
  }
  return true;
}

function validateLowerUpperTextBox(lowerId, upperId, errmsg) {
  var txtLower = document.getElementById(lowerId);
  var txtUpper = document.getElementById(upperId);
  if (txtLower && txtUpper) {
    var a = parseInt(txtLower.value);
    var b = parseInt(txtUpper.value);
    if (a > b) {
      alert(errmsg);
      txtUpper.focus();
      return cancelEvent();
    }
  }
  return true;
}

function validateRequireRadioGroup(groupName, errmsg) {
  var rdo = document.getElementsByName(groupName);
  if (rdo && rdo.length>0) {
    var hasChecked = false;
    for (var i=0; i<rdo.length; ++i) {
      if (rdo[i].checked) {
        hasChecked = true;
        break;
      }
    }
    if (!hasChecked) {
      alert(errmsg);
      rdo[0].focus();
      return cancelEvent();
    }
  }
  return true;
}

function validateRequireSelectCombo(id, errmsg) {
  var cbo = document.getElementById(id);
  if (cbo && cbo.options.length>0 && cbo.options[0].selected) {
    alert(errmsg);
    cbo.focus();
    return cancelEvent();
  }
  return true;
}

// + For KeyPress Data + //
function filterInput(filterType,evt,allowDecimal,decNum)
{
	var keyCode, Char, inputField, filter = ''; 
	var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
	var num   = '0123456789'; 
	var dateSymbol = '/';
	var timeSymbol = ':';
	if(window.event)
	{ 
		keyCode = window.event.keyCode; 
		evt = window.event; 
	}else if (evt)keyCode = evt.which; 
	else return true;
	if(filterType == 0) filter = alpha; 
	else if(filterType == 1) filter = num; 
	else if(filterType == 2) filter = alpha + num; 
	else if(filterType == 3) filter = num + dateSymbol;
	else if(filterType == 4) filter = num + dateSymbol + '-';
	else if(filterType == 5) filter = num + timeSymbol;
	else if(filterType == 6) filter = num + '-';
	if(filter == '')return true; 
	inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget; 	
	if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27))return true; 
	Char = String.fromCharCode(keyCode); 
	if((filter.indexOf(Char) > -1))
	{
		if (decNum && inputField.value.indexOf('.') > -1)
		{
			var str = inputField.value;
			var afterDec = str.substring(inputField.value.indexOf('.'));
			var currDecNum =afterDec.length;
			if (currDecNum <= decNum)
			{
				return true;
			}else
			{
				return false;
			}
		}else
		{
			return true;
		}
		return true;
	}else if(filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)
		return true;
	else return false; 
}

function numberOnly(event)
{
	return filterInput(1, event);
}
function dateOnly(event)
{
	return filterInput(3, event);
}
function decimalLimitedOnly(event, len) {
   return filterInput(1, event, true, len);
}

function getUrlVars() {
   var vars = [], hash;
   var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
   for (var i = 0; i < hashes.length; i++) {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
   }
   return vars;
}